﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TETRIS_v01.GameHost;

namespace TETRIS_v01.FieldObjects
{
    public class Field
    {        
        public int FieldWidth { get; protected set; }
        public int FieldHeight { get; protected set; }
        public Coordinates StartPoint { get; protected set; }
        public ConsoleColor FrameColor { get; protected set; }
        public int[,] _gameCellsStatus; //1 - busy, 0 - empty
        

        private Coordinates[] _fieldCells; //поле нужно для метода ClearField2()
        
        //private List<Shape> _shapes = new List<Shape>();
        
        public Field(int fieldWidth, int fieldHeight, ConsoleColor frameColor)
        {
            StartPoint = new Coordinates(16, 1);
            FieldWidth = fieldWidth;
            FieldHeight = fieldHeight;
            FrameColor = frameColor;

            int numOfCells = fieldWidth * fieldHeight; // *блок нужен для метода ClearField2()
            _fieldCells = new Coordinates[numOfCells];
            int i = 0;
            for (int y = StartPoint.y+1; y <= StartPoint.y + fieldHeight; y++)
            {
                for (int x = StartPoint.x+1; x <= StartPoint.x + fieldWidth; x++)
                {
                    _fieldCells[i] = new Coordinates(x, y);
                    i++;
                }
            } // *

            _gameCellsStatus = new int[fieldWidth, fieldHeight];
            for (int x = 0; x < fieldWidth; x++)
            {
                for (int y = 0; y < fieldHeight; y++)
                {
                    _gameCellsStatus[x, y] = 0;
                }
            }
        }

        public void PrintField(ConsoleColor fallenShapesColor)
        {
            Console.CursorVisible = false;
            for (int y = 0; y < FieldHeight; y++)
            {
                for (int x = 0; x < FieldWidth; x++)
                {
                    if (_gameCellsStatus[x, y] == 1)
                    {
                        Block b = new Block(x + StartPoint.x + 1, y + StartPoint.y + 1, fallenShapesColor);
                        b.PrintBlock();
                    }
                }                
            }
        }

        public void ClearField()
        {
            Console.CursorVisible = false;
            for (int y = 0; y < FieldHeight; y++)
            {
                for (int x = 0; x < FieldWidth; x++)
                {
                    if (_gameCellsStatus[x, y] == 0)
                    {
                        Console.SetCursorPosition(x + StartPoint.x + 1, y + StartPoint.y + 1);
                        Console.Write(" ");
                    }
                }
            }
        }       
        
        public void ClearField2()
        {
            foreach (Coordinates c in _fieldCells)
            {
                c.Clear(c.x,c.y);
            }
        }

        public List<int> FindFullRows()
        {
            List<int> fullRowsNumbers = new List<int> (); 
            for (int y = 0; y < FieldHeight; y++)
            {
                int countCells = 0;
                for (int x = 0; x < FieldWidth; x++)
                {
                    if (_gameCellsStatus[x, y] == 1)
                    {
                        countCells++;
                    }
                    else break;
                }
                if (countCells == FieldWidth)
                {
                    fullRowsNumbers.Add(y);
                    /*Console.SetCursorPosition(1, StartPoint.y+1+y);
                    Console.Write("Row {0} is full", y);*/                  
                } // row is full
            }
            return fullRowsNumbers;
        }

        public void PrintFullRows(List<int> rowsNumbers, ConsoleColor fullRowColor)
        {
            foreach (int y in rowsNumbers) // y == row number;
            {
                for (int x = 0; x < FieldWidth; x++)
                {
                    Block b = new Block(x + StartPoint.x + 1, y + StartPoint.y + 1, fullRowColor, "☺");
                    b.PrintBlock();
                }
            }
        }

        public void DeleteFullRows(List<int> rowsNumbers)
        {
            if (rowsNumbers.Count() > 0)
            {
                foreach (int row in rowsNumbers)
                {
                    DeleteOneRow(row);
                }
            }
            // delete full rows and return new fieldSellsStatus
        }

        private void DeleteOneRow(int rowNumber)
        {
            //_gameCellsStatus;
            for (int y = rowNumber; y > 0; y--)
            {
                for (int x = 0; x < FieldWidth; x++)
                {
                    _gameCellsStatus[x, y] = _gameCellsStatus[x, y - 1];
                }
            }
            for (int x = 0; x < FieldWidth; x++)
            {
                _gameCellsStatus[x, 0] = 0;
            }
        }
    }
}
